var list=null;
$(function(){
	tabId=$("#tabContent > .tab-pane.active").attr("id");	
	//判断补录日监测数据
	$.post(contextPath +"/fileUpload/getNoDataDay.dhc",{'hospCode':paramTabPage[tabId].hospCode},function(result){
		list = result.data;
		if(list.length!=0){
			 drgUI.confirm({
				content : '2个月内，有以下日期需要进行数据补录：<p>'+JSON.stringify(list)+'</p>点击“确定”开始数据补录，点击“继续”继续异常数据查询',
				btnl:'确定',
				btnr:'继续',
				callSure : function() {
					 dataIn();
				},
				callCancel : function() {
					datables();
				}
			});
		}
	});
	/**
	 * 提交点击事件
	 */	
	$("#submitBtn").on('click', function() {
		var outDep = $('#output_dep').val().split(",")[0];
		var hospCode = "";
		if($('#output_dep').val().split(",")[1]){
			hospCode = $('#output_dep').val().split(",")[1];
		}
		var isAbnormal = $('#is_abnormal').val();
		var drEpisodeId = $('#drEpisodeId').val();
		if (outDep==''||isAbnormal=='') {
			drgUI.warning('请选择科室或状态!');
			return;
		} else {
			$.post(contextPath+'/DailyRecord/saveOrUpdate.dhc',
			    {
				'drOutDep':outDep,
				'hospCode':hospCode,
				'drIsAbnormal':isAbnormal,
				'drEpisodeId':drEpisodeId
				},
			function(){
				drgUI.success('修改成功!');
				$('#editDepModal').modal('hide');
				datables();
			});
		}
	});	
});
/**
 *  获取科室信息
 */
$.post(contextPath +"/DrgsIndexCal/findAllDepByHospCode.dhc",paramTabPage[tabId],function(dep){	
	$("#input_dep,#output_dep").html('<option value="" style="color:red;">请修改</option>');
	$.each(dep.data,function(i,n){
		$("#input_dep,#output_dep").append('<option value="'+n.DEPCODE+','+n.HOSPCODE+'">'+n.DEPNAME+'('+n.HOSPNAME+')</option>');
	});
});
/**
 * 
 * @ Discription:补录数据
 * @Autor ：liuhongjun
 * 2017年6月18日下午9:00:52
 */
function dataIn(){
	//监测日监测接口是否存在
	$.post(contextPath +"/DailyRecord/findDailyRecordInterFace.dhc",function(result){
		if(result.success){
			$.post(contextPath +"/DailyRecord/daysDataImport.dhc",{
			'method':result.data.method,
			'wsUrl':result.data.wsUrl,
			'hospCode':paramTabPage[tabId].hospCode
			},function(result){
				if(result.success){
					drgUI.success('成功导入'+result.data+'天的数据');
				}
				else{
					drgUI.error(result.errorMessage);
				}
			});	
		}else{
			drgUI.error('请先联系管理员配置日监测接口信息！');
		}
	
	});	
}
function checkMsg(str){
	//alert(str);
	var json = $CommonUI.loopBlock('detail');
	var drEpisodeId = json['dto.dailyRecord.drEpisodeId'];
	var drInDep = json['dto.dailyRecord.drInDep'];
	var drOutDep = json['dto.dailyRecord.drOutDep'];
	var drIsAbnormal = json['dto.dailyRecord.drIsAbnormal'];
	var CheckMsg="";
	if(drEpisodeId=="") CheckMsg += "编码不能为空!</br>";
	if(drInDep=="") CheckMsg += "入院科室不能为空!</br>";
	if(drOutDep=="") CheckMsg += "出院科室不能为空!</br>";
	if(drIsAbnormal=="") CheckMsg += "更改后状态不能为空!</br>";
	return CheckMsg;
	
};
/**
 * 修正科室信息
 * @param {} drEpisodeId
 * @param {} drInDep
 * @param {} drOutDep
 */
function editRow(drEpisodeId,drInDep,drOutDep) {
	$("#drEpisodeId").val(drEpisodeId);
	$("#is_abnormal").val('2');	
	$("#input_dep").val(drInDep);
	$("#output_dep").val(drOutDep);
	$("#his_in_dep").html(drInDep);
	$("#his_out_dep").html(drOutDep);
	$('#editDepModal').modal('show');
}

function importData(){
	postReq(contextPath+'/DailyRecord/daysDataImport.dhc',function(){
	});
}


function succ(data){
	var drEpisodeId=$('#detail input[name="dto.dailyRecord.drEpisodeId"]').val();	
	if(drEpisodeId){
		drgUI.success("更新科室信息成功");
	}
	$("#patientInfo").dialog('close');
	datables();

}
//新增或更新失败的回调函数
function err(xhr,textStatus,errorThrown){
	drgUI.error("更新科室信息失败");
}
/**
 * 绘制异常数据表格
 */
function datables(){	
	$.post(contextPath+'/DailyRecord/findDailyRecords.dhc',{'hospCode':paramTabPage[tabId].hospCode},function(data){
		var columns=[ 
 	                     { "data": "drEpisodeid",
 	                     	"title":"ID"
 	                     },
 	                     { "data": "drMrno",
 	                       "title":"病案号"
 	                     },
 	                     { "data": "drPatientname",
 	                       "title":"患者姓名"
 	                     },
 	                     { "data": "drPatienttype",
 	                       "title":"患者类型"
 	                       },
 	                     { "data": "drInhisdep" ,
 	                       "title":"入院科室"
 	                       },
	                     { "data": "drOuthisdep",
 	                       "title":"出院科室"
	                     },
	                     { "data": "drOutdate1" ,
	                     	"title":"出院时间",
	                    	 "render": function(obj, type, full) {
	                             return new Date(full.drOutdate).format("yyyy-MM-dd");
	                         }
	                     },
	                     { "data": "drIsabnormal" ,
	                        "title":"数据是否异常",
	                    	 "render": function(obj, type, full) {
	                             var s = "是";
	                             if ( full.drIsabnormal == 1 ) {
	                            	 return s;
	                             }else{return "否";}
	                             
	                         }
	                     } ,
	                     { "data": "" ,
	                       "title":"点击修改",
	                    	"render": function(obj, type, full) { 
	                            return '<a href="javascript:editRow(\''+full.drEpisodeid+'\',\''+full.drInhisdep+'\',\''+full.drOuthisdep+'\')"><i class=\"icon-pencil\"></i></a>';
	                         }
	                     }
 	                 ];
		var paramsDataTables={order:[[ 1, "acs" ]]};
		$("#"+tabId+" #daymonitor_table").dataTableBasic(columns,data,paramsDataTables);

 	});
}

